package com.example.shop_car_2208.login

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.shop_car_2208.entity.ParamLogin
import com.example.shop_car_2208.entity.UserEntityResult
import com.example.shop_car_2208.mvvm.BaseViewModel
import kotlinx.coroutines.launch

/**
 * 登录Viewmodel
 */
class LoginViewModel:BaseViewModel() {

    val loginSuccess = MutableLiveData<UserEntityResult>()
    val loginFail = MutableLiveData<String>()

    fun Login(username:String,password:String){
        //viewmodel作用域 用来调用挂起函数
        viewModelScope.launch {
            //try-catch 手动添加报错的可能 用来接受请求失败
            try {
                val param = ParamLogin(username,password)
                val result = apiService.getLogin(param)
                loginSuccess.value = result
            }catch(e:Exception){
                //调用失败---没有网的时候
                loginFail.value = e.message
        }
        }
    }
}